package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.LocationService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.entity.driver.DriverSet;
import com.atguigu.daijia.model.form.map.OrderServiceLocationForm;
import com.atguigu.daijia.model.form.map.UpdateDriverLocationForm;
import com.atguigu.daijia.model.form.map.UpdateOrderLocationForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 谢忠涵7
 */
@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class LocationServiceImpl implements LocationService {
    @Autowired
    private LocationFeignClient locationFeignClient;
    @Autowired
    private DriverInfoFeignClient driverInfoFeignClient;
    /**
     *开启接单服务：更新司机经纬度位置
     */
    @Override
    public Boolean updateDriverLocation(UpdateDriverLocationForm updateDriverLocationForm) {
        Result<DriverSet> driverSet = driverInfoFeignClient
                .getDriverSet(updateDriverLocationForm.getDriverId());
        int status = driverSet.getData().getServiceStatus();
        if(status != 1) {
            throw new GuiguException(ResultCodeEnum.NO_START_SERVICE);
        }
        Result<Boolean> result = locationFeignClient.updateDriverLocation(updateDriverLocationForm);
        return result.getData();
    }

    /**
     * 司机赶往代驾起始点：更新订单位置到Redis缓存
     */
    @Override
    public Boolean updateOrderLocationToCache(UpdateOrderLocationForm updateOrderLocationForm) {
        Result<Boolean> result = locationFeignClient.updateOrderLocationToCache(updateOrderLocationForm);
        return result.getData();
    }

    /**
     * 开始代驾服务：保存代驾服务订单位置
     */
    @Override
    public Boolean saveOrderServiceLocation(List<OrderServiceLocationForm> orderLocationServiceFormList) {
        Result<Boolean> result = locationFeignClient.saveOrderServiceLocation(orderLocationServiceFormList);
        return result.getData();
    }
}
